package alg;

import java.util.Arrays;

//购物车内的商品价格按照升序记录于数组 price。请在购物车中找到两个商品的价格总和刚好是 target。若存在多种情况，返回任一结果即可。
//输入：price = [3, 9, 12, 15], target = 18
//输出：[3,15] 或者 [15,3]
//输入：price = [8, 21, 27, 34, 52, 66], target = 61
//输出：[27,34] 或者 [34,27]
public class Solution {
    public int[] twoSum(int[] price, int target) {
        int left = 0,right = price.length - 1;
        while(left < right){
            int sum = 0 ;
            sum = price[left] + price[right];
            if(sum < target){
                left++;
            }else if(sum > target){
                right--;
            }else{
                return new int[] {price[left],price[right]};
            }

        }
        return new int[]{0};
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int [] nums = {8, 21, 27, 34, 52, 66};
        System.out.println(Arrays.toString(solution.twoSum(nums,61)));
    }
}